package cloud.maque.auth.controller;

import cloud.maque.auth.entity.AuthUser;
import cloud.maque.auth.service.UserService;
import cloud.maque.auth.service.UserThridService;
import cloud.maque.common.core.response.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户的相关操作")
@RestController
@RequestMapping("/auth/user")
public class AuthUserController {


    @Autowired
    UserService userService;

    @Autowired
    UserThridService thridService;

    @Operation(summary = "通过OpenId获取用户")
    @PostMapping("/get-user-by-openid/{openid}")
    public R GetUserByOpenId(@PathVariable("openid") String openid) {
        AuthUser result = userService.GetUserByOpenId(openid);
        return R.ok("登陆成功", result);
    }


    @Operation(summary = "通过第三方获取用户")
    @PostMapping("/get-user-by-thrid/{thirdType}/{thridUserId}")
    public R<AuthUser> GetUserByThrid(@PathVariable("thirdType") String thirdType, @PathVariable("thridUserId") String thridUserId) {
        AuthUser result = thridService.findByUser(thirdType, thridUserId);
        return R.ok("查询成功", result);
    }

    @Operation(summary = "获取用户信息")
    @PostMapping("/get-userinfo/{userId}")
    public R<AuthUser> GetUserInfo(@PathVariable("userId") String userId) {
        AuthUser result = userService.getUserByUserId(userId);
        return R.ok("查询成功", result);
    }

    @Operation(summary = "根据手机号获取用户")
    @PostMapping("/get-by-phone/{phone}")
    public R<AuthUser> GetUserInfoByPhone(@PathVariable("phone") String phone) {
        AuthUser result = userService.getUserByUserPhone(phone);
        if (result != null) {
            return R.ok("查询成功", result);
        } else {
            return R.error("手机号不存在");
        }
    }

    @Operation(summary = "根据用户的Totp的密钥")
    @PostMapping("/get-user-totp-secret/{userId}")
    public R<String> getUserTotpSecret(@PathVariable("userId") String userId) {
        String result = userService.getUserTotpSecret(userId);
        return R.ok("查询成功", result);
    }

    @Operation(summary = "根据用户的Totp的密钥")
    @PostMapping("/update-user-totp-secret")
    public R<Boolean> updateUserTotpSecret(@RequestParam("userId") String userId, @RequestParam("totpSecret") String totpSecret) {
        Boolean result = userService.updateUserTotpSecret(userId, totpSecret);
        return R.ok("更新成功", result);
    }
}
